﻿using System;
using System.Web.UI;
using Entities;
using Business;
namespace ServicesDirectory.UserControl
{
    /// <summary>
    /// 
    /// </summary>
    public partial class OrganisationDetail : System.Web.UI.UserControl
    {

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            lbtDetail1.Click += LbtDetail1_Click;
            lbtDetail2.Click += LbtDetail2_Click;
            lbtDetail3.Click += LbtDetail3_Click;
            lbtDetail4.Click += LbtDetail4_Click;
            lbtDetail5.Click += LbtDetail5_Click;
            lbtDetail6.Click += LbtDetail6_Click;
            OrganisationDetail1.ExpressionCheckedChange += OrganisationDetail1_ExpressionCheckedChange;
            cmdBack.Click += CmdBackClick;
            btnInactive.Click += BtnInactive_Click;

            const string draggable = "$( '#draggable' ).draggable();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "OrganisationDetailKeyContact", draggable, true);
            string orgId = Request.QueryString["orgId"];
            if (!IsPostBack)
                if (orgId == null)
                {
                    ViewState["ModeForm"] = "Add";
                    lbtDetail3.Visible = false;
                    lbtDetail4.Visible = false;
                    lbtDetail5.Visible = false;
                    lbtDetail6.Visible = false;
                    pnDetail1.Visible = true;
                    pnDetail2.Visible = false;
                    pnDetail3.Visible = false;
                    pnDetail4.Visible = false;
                    pnDetail5.Visible = false;
                    pnDetail6.Visible = false;
                    btnInactive.Visible = false;

                }
                else
                {
                    OrganisationDetail2.SetData += OrganisationDetail2_SetData;
                    OrganisationDetail3.SetDataDetail3 += OrganisationDetail3_SetDataDetail3;
                    OrganisationDetail5.SetDataDetail5 += OrganisationDetail5_SetDataDetail5;
                    ListDirectorate1.SetDataDetail6 += ListDirectorate1_SetDataDetail6;
                    if (Request.QueryString["isActive"] != null)
                        OrganizationBusiness.Instance.MarkInactive(orgId);
                    ViewState["orgId"] = orgId;
                    Session["orgId"] = orgId;
                    ViewState["ModeForm"] = "Edit";
                    //bind Data
                    BindData(orgId);

                    lbtDetail1.Visible = true;
                    lbtDetail2.Visible = true;
                    lbtDetail4.Visible = true;
                    lbtDetail5.Visible = true;
                    lbtDetail6.Visible = true;
                    pnDetail1.Visible = true;
                    pnDetail2.Visible = false;
                    pnDetail3.Visible = false;
                    pnDetail4.Visible = false;
                    pnDetail5.Visible = false;
                    pnDetail6.Visible = false;
                    btnInactive.Visible = true;

                    if (Request.QueryString["inactive"] != null)
                    {
                        var script = "";
                        var org = new Organisation();
                        org.OrgId = new Guid(Request.QueryString["inactive"]);
                        OrganisationDetail1.GetData();
                        org.AddressLine1 = OrganisationDetail1.AddressLine1;
                        org.AddressLine2 = OrganisationDetail1.AddressLine2;
                        org.AddressLine3 = OrganisationDetail1.AddressLine3;
                        org.BusinessId = OrganisationDetail1.BusinessId;
                        org.CharityNumber = OrganisationDetail1.CharityNumber;
                        org.CompanyNumber = OrganisationDetail1.CompanyNumber;
                        org.ContactId = OrganisationDetail1.ContactId;
                        org.Email = OrganisationDetail1.Email;
                        org.ExpressionOfBusiness = OrganisationDetail1.ExpressionOfBusiness;
                        org.Fax = OrganisationDetail1.Fax;
                        org.OrgFullDescription = OrganisationDetail1.OrgFullDescription;
                        org.OrgName = OrganisationDetail1.OrgName;
                        org.OrgPreferred = OrganisationDetail1.OrgPreferred;
                        org.PhoneNumber = OrganisationDetail1.PhoneNumber;
                        org.ShortDescription = OrganisationDetail1.ShortDescription;
                        org.WebAddress = OrganisationDetail1.WebAddress;
                        org.AddressId = OrganisationDetail1.AddressId;

                        OrganisationDetail2.GetData();

                        org.Accreditation = OrganisationDetail2.Accreditation;
                        org.ServiceBarriers = OrganisationDetail2.ServiceBarriers;
                        org.ServiceBenefits = OrganisationDetail2.ServiceBenefits;
                        org.ServiceDisability = OrganisationDetail2.ServiceDisability;
                        org.ServiceEthnicity = OrganisationDetail2.ServiceEthnicity;
                        org.ServicePersonalCircumstances = OrganisationDetail2.ServicePersonalCircumstances;
                        org.Specialism = OrganisationDetail2.Specialism;
                        OrganisationDetail3.GetData();
                        org.EoiProgrammes = OrganisationDetail3.EoiProgrammes;
                        org.EoiService = OrganisationDetail3.EoiService;
                        org.IsActive = false;
                        if (OrganizationBusiness.Instance.UpdateOrganisation(org))
                        {
                            script =
                                "var result= window.confirm('Mark InActive successfull! Do you want back page ListOrganisation!'); " +
                                "if(result)" +
                                " location.href=('ListOrganisation.aspx');"
                                + "else"
                                + "location.href=('OrganisationDetail.aspx?orgId="+org.OrgId.ToString()+"');";
                        }
                        else
                        {
                            script = "alert('Mark InActive fail!');";

                        }
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof (UpdatePanel),
                                                                "MarkInactiveOrganisation1", script, true);
                    }

                }
            //cmdBack.Attributes.Add("onClick", "javascript:history.back(); return false;");
            
        }
        /// <summary>
        /// Handles the SetDataDetail6 event of the ListDirectorate1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ListDirectorate1_SetDataDetail6(object sender, EventArgs e)
        {
            if (ViewState["orgId"] != null)
            {
                ListDirectorate1.Display(new Guid(ViewState["orgId"].ToString()));
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void BtnInactive_Click(object sender, EventArgs e)
        {

            var script = "";
            if (ViewState["orgId"] != null)
            {
                script = "var result= window.confirm('Do you want to make this Service in-active?'); " +
                         "if(result)" +
                         " location.href=('OrganisationDetail.aspx?inactive=" + ViewState["orgId"] + "&orgId=" + ViewState["orgId"] + "');";
            }
            else
            {
                script = "Organisation not found! please back listOrganisation!";
            }
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "MarkInactiveOrganisation", script, true);
        }

        /// <summary>
        /// Handles the SetDataDetail5 event of the OrganisationDetail5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void OrganisationDetail5_SetDataDetail5(object sender, EventArgs e)
        {
            if (ViewState["orgId"] != null)
            {
                var organisation = OrganizationBusiness.Instance.GetOrganisationById(ViewState["orgId"].ToString());
                if (organisation != null)
                {
                    OrganisationDetail5.BindData(organisation.OrgId.ToString(), true);
                }
            }
        }

        /// <summary>
        /// Handles the SetDataDetail3 event of the OrganisationDetail3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void OrganisationDetail3_SetDataDetail3(object sender, EventArgs e)
        {
            //bind organisation detail3
            if (ViewState["orgId"] != null)
            {
                var organisation = OrganizationBusiness.Instance.GetOrganisationById(ViewState["orgId"].ToString());
                if (organisation != null)
                {
                    OrganisationDetail3.EoiProgrammes = organisation.EoiProgrammes;
                    OrganisationDetail3.EoiService = organisation.EoiService;
                    OrganisationDetail3.LoadData();
                }
            }
        }

        /// <summary>
        /// Handles the SetData event of the OrganisationDetail2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void OrganisationDetail2_SetData(object sender, EventArgs e)
        {
            if (ViewState["orgId"] != null)
            {
                var organisation = OrganizationBusiness.Instance.GetOrganisationById(ViewState["orgId"].ToString());
                if (organisation != null)
                {
                    OrganisationDetail2.Accreditation = organisation.Accreditation;
                    OrganisationDetail2.Specialism = organisation.Specialism;
                    OrganisationDetail2.ServiceBarriers = organisation.ServiceBarriers;
                    OrganisationDetail2.ServiceBenefits = organisation.ServiceBenefits;
                    OrganisationDetail2.ServiceDisability = organisation.ServiceDisability;
                    OrganisationDetail2.ServiceEthnicity = organisation.ServiceEthnicity;
                    OrganisationDetail2.ServicePersonalCircumstances = organisation.ServicePersonalCircumstances;
                    OrganisationDetail2.LoadData();
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void CmdBackClick(object sender, EventArgs e)
        {
            Response.Redirect("ListOrganisation.aspx", true);
        }

        /// <summary>
        /// Handles the ExpressionCheckedChange event of the OrganisationDetail1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void OrganisationDetail1_ExpressionCheckedChange(object sender, EventArgs e)
        {
            if ((bool)sender)
            {
                lbtDetail1.CssClass = "";
                lbtDetail2.CssClass = "";
                lbtDetail3.CssClass = "active";
                lbtDetail3.Visible = true;
                lbtDetail4.CssClass = "";
                lbtDetail5.CssClass = "";
                lbtDetail6.CssClass = "";

                pnDetail1.Visible = false;
                pnDetail2.Visible = false;
                pnDetail3.Visible = true;
                pnDetail4.Visible = false;
                pnDetail5.Visible = false;
                pnDetail6.Visible = false;
              

            }
            else
            {
                lbtDetail3.Visible = false;
                pnDetail3.Visible =false;
            }
        }

        /// <summary>
        /// Handles the Click event of the LbtDetail6 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtDetail6_Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "active";

            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = true;

        }

        /// <summary>
        /// Handles the Click event of the LbtDetail5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtDetail5_Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "active";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = true;
            pnDetail6.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the LbtDetail4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtDetail4_Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "active";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = true;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the LbtDetail3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtDetail3_Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "active";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = true;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the LbtDetail2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtDetail2_Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "active";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = true;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the LbtDetail1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtDetail1_Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "active";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtDetail6.CssClass = "";


            pnDetail1.Visible = true;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnDetail6.Visible = false;
        }

        /// <summary>
        /// CMDs the save click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdSaveClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ViewState["ModeForm"].ToString()) || ViewState["ModeForm"].ToString().Equals("Add"))
            {
                var org = new Organisation();
                org.OrgId = Guid.NewGuid();
                OrganisationDetail1.GetData();
                org.AddressLine1 = OrganisationDetail1.AddressLine1;
                org.AddressLine2 = OrganisationDetail1.AddressLine2;
                org.AddressLine3 = OrganisationDetail1.AddressLine3;
                org.BusinessId = OrganisationDetail1.BusinessId;
                org.CharityNumber = OrganisationDetail1.CharityNumber;
                org.CompanyNumber = OrganisationDetail1.CompanyNumber;
                org.ContactId = OrganisationDetail1.ContactId;
                org.Email = OrganisationDetail1.Email;
                org.ExpressionOfBusiness = OrganisationDetail1.ExpressionOfBusiness;
                org.Fax = OrganisationDetail1.Fax;
                org.OrgFullDescription = OrganisationDetail1.OrgFullDescription;
                org.OrgName = OrganisationDetail1.OrgName;
                org.OrgPreferred = OrganisationDetail1.OrgPreferred;
                org.PhoneNumber = OrganisationDetail1.PhoneNumber;
                org.ShortDescription = OrganisationDetail1.ShortDescription;
                org.WebAddress = OrganisationDetail1.WebAddress;
                org.AddressId = OrganisationDetail1.AddressId;

                OrganisationDetail2.GetData();

                org.Accreditation = OrganisationDetail2.Accreditation;
                org.ServiceBarriers = OrganisationDetail2.ServiceBarriers;
                org.ServiceBenefits = OrganisationDetail2.ServiceBenefits;
                org.ServiceDisability = OrganisationDetail2.ServiceDisability;
                org.ServiceEthnicity = OrganisationDetail2.ServiceEthnicity;
                org.ServicePersonalCircumstances = OrganisationDetail2.ServicePersonalCircumstances;
                org.Specialism = OrganisationDetail2.Specialism;


                OrganisationDetail3.GetData();
                org.EoiProgrammes = OrganisationDetail3.EoiProgrammes;
                org.EoiService = OrganisationDetail3.EoiService;
                org.IsActive = false;
                if (OrganizationBusiness.Instance.InsertOrganisation(org))
                {
                   var script = "var result= window.confirm('Insert Successfull! Do you want to back list organisation?'); " +
                          "if(result)" +
                          " location.href=('ListOrganisation.aspx');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "addfailsaddnew", script, true);

                    btnInactive.Visible = true;
                    if (OrganisationDetail1.ExpressionOfBusiness)
                        lbtDetail3.Visible = true;
                    lbtDetail4.Visible = true;
                    lbtDetail5.Visible = true;
                    lbtDetail6.Visible = true;
                }
                else
                {
                    var script = @"alert('Insert fail!');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "addfailssupdate", script, true);

                }


            }
            else
            {
                if (ViewState["orgId"] != null)
                {
                    var org = new Organisation();
                    org.OrgId = new Guid(ViewState["orgId"].ToString());
                    OrganisationDetail1.GetData();
                    org.AddressLine1 = OrganisationDetail1.AddressLine1;
                    org.AddressLine2 = OrganisationDetail1.AddressLine2;
                    org.AddressLine3 = OrganisationDetail1.AddressLine3;
                    org.BusinessId = OrganisationDetail1.BusinessId;
                    org.CharityNumber = OrganisationDetail1.CharityNumber;
                    org.CompanyNumber = OrganisationDetail1.CompanyNumber;
                    org.ContactId = OrganisationDetail1.ContactId;
                    org.Email = OrganisationDetail1.Email;
                    org.ExpressionOfBusiness = OrganisationDetail1.ExpressionOfBusiness;
                    org.Fax = OrganisationDetail1.Fax;
                    org.OrgFullDescription = OrganisationDetail1.OrgFullDescription;
                    org.OrgName = OrganisationDetail1.OrgName;
                    org.OrgPreferred = OrganisationDetail1.OrgPreferred;
                    org.PhoneNumber = OrganisationDetail1.PhoneNumber;
                    org.ShortDescription = OrganisationDetail1.ShortDescription;
                    org.WebAddress = OrganisationDetail1.WebAddress;
                    org.AddressId = OrganisationDetail1.AddressId;

                    OrganisationDetail2.GetData();

                    org.Accreditation = OrganisationDetail2.Accreditation;
                    org.ServiceBarriers = OrganisationDetail2.ServiceBarriers;
                    org.ServiceBenefits = OrganisationDetail2.ServiceBenefits;
                    org.ServiceDisability = OrganisationDetail2.ServiceDisability;
                    org.ServiceEthnicity = OrganisationDetail2.ServiceEthnicity;
                    org.ServicePersonalCircumstances = OrganisationDetail2.ServicePersonalCircumstances;
                    org.Specialism = OrganisationDetail2.Specialism;


                    OrganisationDetail3.GetData();
                    org.EoiProgrammes = OrganisationDetail3.EoiProgrammes;
                    org.EoiService = OrganisationDetail3.EoiService;
                    org.IsActive = true;
                    if (OrganizationBusiness.Instance.UpdateOrganisation(org))
                    {
                        var script = "var result= window.confirm('Update Successfull! Do you want to back list organisation?'); " +
                          "if(result)" +
                          " location.href=('ListOrganisation.aspx');";
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "addfailssjhaupdatee", script, true);
                    }
                    else
                    {
                        const string script = @"alert('Update fail!');";
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "addfailssjhaupdaten", script, true);
                    }
                }
            }
        }
        /// <summary>
        /// Binds the data.
        /// </summary>
        /// <param name="orgId">The org id.</param>
        void BindData(string orgId)
        {
            var organisation = OrganizationBusiness.Instance.GetOrganisationById(orgId);
            if (organisation != null)
            {
                //bind organisation detai1
                OrganisationDetail1.AddressId = organisation.AddressId;
                OrganisationDetail1.AddressLine1 = organisation.AddressLine1;
                OrganisationDetail1.AddressLine2 = organisation.AddressLine2;
                OrganisationDetail1.AddressLine3 = organisation.AddressLine3;
                OrganisationDetail1.BusinessId = organisation.BusinessId;
                OrganisationDetail1.CharityNumber = organisation.CharityNumber;
                OrganisationDetail1.CompanyNumber = organisation.CompanyNumber;
                OrganisationDetail1.ContactId = organisation.ContactId;
                OrganisationDetail1.Email = organisation.Email;
                OrganisationDetail1.ExpressionOfBusiness = organisation.ExpressionOfBusiness;
                if (organisation.ExpressionOfBusiness)
                {
                    lbtDetail3.Visible = true;
                }
                OrganisationDetail1.Fax = organisation.Fax;
                OrganisationDetail1.OrgFullDescription = organisation.OrgFullDescription;
                OrganisationDetail1.OrgName = organisation.OrgName;
                OrganisationDetail1.OrgPreferred = organisation.OrgPreferred;
                OrganisationDetail1.WebAddress = organisation.WebAddress;
                OrganisationDetail1.ShortDescription = organisation.ShortDescription;
                OrganisationDetail1.PhoneNumber = organisation.PhoneNumber;
                OrganisationDetail1.LoadData();


                //bind organisation detail4               

            }
        }


    }

}